from django.db import models


# Create your models here.

# class BookInfoManage(models.Manager):
#     def all(self):
#         return super().filter(is_delete=False)
#
#     def create_book(self, btitle, bpub_date):
#         # 先获取图书模型类对象
#         book = self.model()
#
#         book.btitle = btitle
#         book.bpub_date = bpub_date
#         book.bread = 20
#         book.save()

        # return book


class BookInfo(models.Model):
    btitle = models.CharField(max_length=20, verbose_name='书名')
    bpub_date = models.DateField(verbose_name='日期')
    bread = models.IntegerField(default=0, verbose_name='阅读量')
    bcomment = models.IntegerField(default=0, verbose_name='评论量')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')
    image=models.ImageField(upload_to='books',null=True)

    # 控制表
    class Meta:
        db_table = 'tb_books'  # 指定表名
        verbose_name = '图书'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.btitle

    def date(self):
        return self.bpub_date

    date.short_description = '日期'
    date.admin_order_field = 'bread'


class HeroInfo(models.Model):
    GENDER_CHOICES = (
        (0, 'male'),
        (1, 'female')
    )
    hname = models.CharField(max_length=20, verbose_name='名称')
    hgender = models.SmallIntegerField(choices=GENDER_CHOICES, default=0, verbose_name='性别')
    hcomment = models.CharField(max_length=200, null=True, verbose_name='描述信息')
    hbook = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name='图书')  # 外键
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_heros'  # 指定表名
        verbose_name = '英雄'  # admin后端展示
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.hname


    def book(self):
        return self.hbook.btitle